package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.management.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/roles")
public class RoleController {

    @Autowired
    private RoleService roleService;


    @GetMapping
    public Result getRoles(Role  role) {
        PageResult<Role> rolesPage =  roleService.getRoles(role);
        return Result.success(rolesPage);
    }

    @DeleteMapping("/{id}")
    public Result deleteRole(@PathVariable("id") Integer id) {
        roleService.deleteRole(id);
        return Result.success();
    }

    @PostMapping
    public Result addRole(@RequestBody Role role) {
        roleService.addRole(role);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getRoleById(@PathVariable("id") Integer id) {
        Role role = roleService.getRoleById(id);
        return Result.success(role);
    }

    @PutMapping
    public Result updateRole(@RequestBody Role role) {
        roleService.updateRole(role);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list(){
        return Result.success(roleService.list());
    }
}
